package com.lightningtrade.log;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;


@Aspect
@Component
@Slf4j
public class MethodLogAspect {

    /**
     * 自定义@MethodLog注解切点
     */
    @Pointcut("@annotation(com.lightningtrade.log.MethodLog)")
    public void MethodLog() {
        log.info("sfahjsifjiasjfdi asfdajif");
    }

    @Before("MethodLog()")
    public void doBefore(JoinPoint joinPoint) {
        // 记录方法入参
        log.info("Request Method: {}, Requset Param: {}", joinPoint.getSignature(), JSON.toJSONString(joinPoint.getArgs()));
    }

    @AfterReturning(returning = "o", pointcut = "MethodLog()")
    public void afterReturning(Object o) {
        // 记录方法返回值
        log.info("Response Result: {}", JSONObject.toJSONString(o));
    }
}
